<?php
namespace app\sendmsg\model;
use app\BaseModel;
use think\facade\Db;

class CardShopOrder extends BaseModel
{
//定义表名
    protected $name = 'longbing_card_shop_order';
    /**
     * @param $type
     * @return mixed
     *
     * 查询某天的销售金额
     */
    public function toDay($where){
        $today = $this->alias('a')->where($where)->whereDay('create_time')->sum('total_price');
        $yesterday = $this->alias('a')->where($where)->whereDay('create_time', 'yesterday')->sum('total_price');
        if($yesterday==0){
            $proportion = 0;
        }else{
            $proportion = $today/$yesterday*100;
        }
        $up = $today>=$yesterday?1:0;
        return  ['sale_today'=>$today,'up'=>$up ,'sale_proportion'=>round($proportion,2)];
    }
    /**
     * @param $type
     * @return mixed
     *
     * 销售金额
     */
    public function salesSum($where,$time){

        return $this->alias('a')
            ->where($where)
            ->join( 'longbing_card_company b', 'b.id = a.company_id')
            ->field('sum(total_price) as money,a.company_id,b.name')
            ->limit(5)
            ->whereTime('a.create_time',$time)
            ->order('money','desc')
            ->group('a.company_id')
            ->select()->toArray();
    }
    //成交笔数和金额
    public function getGoosSale($where){
        $number= 0;
        $money = 0;
        $data = $this
            ->field('count(to_uid) as number,sum(total_price) as money,to_uid')
            ->where($where)
            ->where('status',1)
            ->where('pay_status',1)
            ->where('order_status',3)
            ->group('to_uid')
            ->select()->toArray();
        if($data){
           foreach ($data as $val){
               $number += $val['number'];
               $money += $val['money'];
           }
            $data=[
                'number'=>$number,
                'money'=>round($money,2)
            ];
        }else{
            $data=[
              'number'=>0,
              'money'=>0
            ];
        }
        return $data;
    }
}